package cn.zn.wx.controller;

import cn.hutool.core.collection.CollUtil;
import cn.zn.framework.activiti.RestApi;
import cn.zn.framework.common.abs.AbstractController;
import cn.zn.framework.common.utils.R;
import cn.zn.modules.budget.inter.BudgetInfoInter;
import cn.zn.modules.pz.enums.ProcessTypeEnum;
import cn.zn.modules.pz.inter.PzInter;
import cn.zn.modules.zzsys.inter.UserInter;
import cn.zn.modules.zzsys.inter.vo.SysUserVO;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@RestController
@Api(tags = "首页数据")
@Slf4j
public class IndexRestController extends AbstractController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private UserInter userInter;
    @Autowired(required = false)
    private PzInter pzInter;
    @Autowired(required = false)
    private BudgetInfoInter budgetInfoInter;
    @Autowired
    private RestApi restApi;

    @GetMapping("myInfo")
    @ApiOperation("我的信息")
    public R myInfo() {
        SysUserVO sysUserVO = userInter.queryByLoginUserId2(loginUser().getUserId());
        return R.ok().put("userInfo", sysUserVO);
    }

    @GetMapping("myTodo")
    @ApiOperation("我的待办")
    public R myTodo() {
        String userCode = loginUser().getUsername();
        Map<String, Object> todo = CollUtil.newHashMap();
        for (ProcessTypeEnum processTypeEnum : ProcessTypeEnum.values()) {
            todo.put(processTypeEnum.getDeployKey(), this.queryTodoCount(userCode, processTypeEnum));
        }
        // 待报销
        long toExpense = pzInter.findCountToExpense(userCode);
        todo.put("toExpense", toExpense);
        // 待还款
        long toRepay = pzInter.findCountToRepay(userCode);
        todo.put("toRepay", toRepay);
        // 待报账
        long toAppoint = pzInter.findCountToAppoint(userCode);
        todo.put("toAppoint", toAppoint);
        // 待审核预算，有审核权限的才查询
        long budgetToAudit = budgetInfoInter.findCountToAudit();
        todo.put("budgetToAudit", budgetToAudit);

        return R.ok().put("todo", todo);
    }

    /**
     * 查询待办数量
     *
     * @param userCode
     * @param processTypeEnum
     * @return
     */
    private Long queryTodoCount(String userCode, ProcessTypeEnum processTypeEnum) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("candidateOrAssigned", userCode);
        jsonObject.put("active", true);
        jsonObject.put("processDefinitionKey", processTypeEnum.getDeployKey());
        JSONObject task = restApi.queryTask(jsonObject.toJSONString());
        if (task.containsKey("total")) {
            return task.getLong("total");
        } else {
            return 0L;
        }
    }

    public R queryTodo(){
        return R.ok();
    }
}
